package com.yyw.foodie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyw.foodie.bo.users.UserLoginBo;
import com.yyw.foodie.bo.users.UserRegisterBo;
import com.yyw.foodie.enums.SexEnums;
import com.yyw.foodie.mapper.UsersMapper;
import com.yyw.foodie.pojo.UsersEntity;
import com.yyw.foodie.service.UsersService;
import com.yyw.foodie.utils.DateUtil;
import com.yyw.foodie.utils.FileUtils;
import com.yyw.foodie.utils.MD5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 用户表  服务实现类
 * </p>
 *
 * @author yiyw
 * @since 2021-05-22
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, UsersEntity> implements UsersService {

    /**
     * 获取用户名是否存在
     *
     * @param userName 用户名
     * @return true/false
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Boolean userNameIsExists(String userName) {
        QueryWrapper<UsersEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UsersEntity::getUserName, userName);
        int count = count(queryWrapper);
        return count == 0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public UsersEntity register(UserRegisterBo userRegisterBo) {
        UsersEntity user = new UsersEntity();
        // 用户名
        user.setUserName(userRegisterBo.getUserName());
        // 昵称默认和用户名一致
        user.setNickName(userRegisterBo.getUserName());
        try {
            // 密码使用MD5加密
            user.setPassWord(MD5Utils.getMD5Str(userRegisterBo.getPassWord()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 设置默认生日
        user.setBirthday(DateUtil.stringToDate("1990-01-01"));
        // 性别默认保密
        user.setSex(SexEnums.保密.code);
        // 头像
        // TODO: 2021/5/22 将头像地址换放在服务器 
        user.setFace("http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png");
        // 保存
        save(user);
        return user;
    }

    /**
     * 用户登录获取用户信息
     *
     * @param userLogin
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public UsersEntity login(UserLoginBo userLogin) throws Exception {
        String passWord = MD5Utils.getMD5Str(userLogin.getPassWord());
        QueryWrapper<UsersEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UsersEntity::getUserName, userLogin.getUserName())
                .eq(UsersEntity::getPassWord, passWord);
        UsersEntity entity = getOne(queryWrapper);
        return entity;
    }

    /**
     * 修改用户头像
     *
     * @param userId userId
     * @param file   头像文件流
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateFace(String userId, MultipartFile file) {
        // 1. 下载文件，获取文件名
        String uploadPath = FileUtils.uploadUserFace(file, userId);
        // 2. 保存至用户信息
        UpdateWrapper<UsersEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(UsersEntity::getFace, uploadPath).eq(UsersEntity::getId, userId);
        update(updateWrapper);
    }
}
